package com.surprise.bweb.manager;

import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Lists;
import com.google.common.collect.Multimap;
import com.surprise.base.util.DateUtils;
import com.surprise.base.util.HtmlUtils;
import com.surprise.biz.BizConstants;
import com.surprise.bweb.APIConstants;
import com.surprise.bweb.vo.AddInfoVO;
import com.surprise.bweb.vo.InfoVO;
import com.surprise.db.dao.CategoryDAO;
import com.surprise.db.dao.InfoDAO;
import com.surprise.db.dao.MallDAO;
import com.surprise.db.entity.BUser;
import com.surprise.db.entity.Mall;
import com.surprise.db.po.Category;
import com.surprise.db.po.Info;
import com.surprise.db.po.User;

/**
 * InfoManager.java
 * @author nick
 * @since 2015年7月25日
 */
@Component
public class InfoManager {
	@Autowired
	private InfoDAO infoDAO;
	@Autowired
	private CategoryDAO categoryDAO;
	@Autowired
	private MallDAO mallDAO;
	
	/**
     * 管理员获取所有
     * @return
     */
	public List<InfoVO> getList() {
        List<InfoVO> ret = Lists.newArrayList();
        List<Info> pos = infoDAO.findAll();
        if (CollectionUtils.isEmpty(pos)) {
            return ret;
        }
        Map<Integer, Category> map = categoryDAO
                .getMapByType(BizConstants.TYPE.EXPERIENCE.type);
        Multimap<Long, InfoVO> mallidInfoMap = ArrayListMultimap
                .create();
        for (Info po : pos) {
            InfoVO vo = new InfoVO();
            vo.setId(po.getId());
            vo.setName(po.getTitle());
            vo.setCouponId(po.getCouponid());
            Category cat = map.get(po.getCategory());
            if (cat != null) {
                vo.setCategory(cat.getName());
            }
            vo.setStartTime(DateUtils.format(po.getStarttime(), APIConstants.PC_TIME_FORMAT));
            vo.setEndTime(DateUtils.format(po.getEndtime(), APIConstants.PC_TIME_FORMAT));
            ret.add(vo);
            mallidInfoMap.put(po.getMallid(), vo);
        }
        Map<Long, Mall> malls = mallDAO.findMapByIds(mallidInfoMap
                .keySet());
        for (Long mallid : mallidInfoMap.keySet()) {
            Mall mall = malls.get(mallid);
            if (mall != null) {
                for (InfoVO item : mallidInfoMap.get(mallid)) {
                    item.setMall(mall.getName());
                    item.setMallId(mall.getId());
                }
            }
        }
        return ret;
    }
	
	/**
     * 商城管理员
     * @param mid
     * @return
     */
	public List<InfoVO> getListByMall(long mid) {
        List<InfoVO> ret = Lists.newArrayList();
        List<Info> pos = infoDAO.findByMallid(mid);
        if (CollectionUtils.isEmpty(pos)) {
            return ret;
        }
        Map<Integer, Category> map = categoryDAO
                .getMapByType(BizConstants.TYPE.EXPERIENCE.type);
        Multimap<Long, InfoVO> mallidInfoMap = ArrayListMultimap
                .create();
        for (Info po : pos) {
            InfoVO vo = new InfoVO();
            vo.setId(po.getId());
            vo.setName(po.getTitle());
            vo.setCouponId(po.getCouponid());
            Category cat = map.get(po.getCategory());
            if (cat != null) {
                vo.setCategory(cat.getName());
            }
            vo.setStartTime(DateUtils.format(po.getStarttime(), APIConstants.PC_TIME_FORMAT));
            vo.setEndTime(DateUtils.format(po.getEndtime(), APIConstants.PC_TIME_FORMAT));
            ret.add(vo);
            mallidInfoMap.put(po.getMallid(), vo);
        }
        Map<Long, Mall> malls = mallDAO.findMapByIds(mallidInfoMap
                .keySet());
        for (Long mallid : mallidInfoMap.keySet()) {
            Mall mall = malls.get(mallid);
            if (mall != null) {
                for (InfoVO item : mallidInfoMap.get(mallid)) {
                    item.setMall(mall.getName());
                    item.setMallId(mall.getId());
                }
            }
        }
        return ret;
    }
	
	public List<InfoVO> getList(long userId) {
		List<InfoVO> ret = Lists.newArrayList();
		List<Info> pos = infoDAO.findByUserid(userId);
		if (CollectionUtils.isEmpty(pos)) {
			return ret;
		}
		Map<Integer, Category> map = categoryDAO
				.getMapByType(BizConstants.TYPE.EXPERIENCE.type);
		Multimap<Long, InfoVO> mallidInfoMap = ArrayListMultimap
				.create();
		for (Info po : pos) {
			InfoVO vo = new InfoVO();
			vo.setId(po.getId());
			vo.setName(po.getTitle());
			vo.setCouponId(po.getCouponid());
			Category cat = map.get(po.getCategory());
			if (cat != null) {
				vo.setCategory(cat.getName());
			}
			vo.setStartTime(DateUtils.format(po.getStarttime(), APIConstants.PC_TIME_FORMAT));
			vo.setEndTime(DateUtils.format(po.getEndtime(), APIConstants.PC_TIME_FORMAT));
			ret.add(vo);
			mallidInfoMap.put(po.getMallid(), vo);
		}
		Map<Long, Mall> malls = mallDAO.findMapByIds(mallidInfoMap
				.keySet());
		for (Long mallid : mallidInfoMap.keySet()) {
			Mall mall = malls.get(mallid);
			if (mall != null) {
				for (InfoVO item : mallidInfoMap.get(mallid)) {
					item.setMall(mall.getName());
					item.setMallId(mall.getId());
				}
			}
		}
		return ret;
	}
	
	public int deleteInfo(long id) {
	    return infoDAO.deleteById(id);
	}
	
	public int deleteInfo(long id, long mallid) {
		return infoDAO.deleteByMall(id, mallid);
	}
	
	public Long add(AddInfoVO vo, BUser user) {
		Mall mall = mallDAO.findById(vo.getMall());
		
		if (mall == null) {
			throw new RuntimeException("mall not exists");
		}
		Info po = new Info();
		po.setCategory(vo.getCategory());
		po.setUserid(user.getId());
		po.setTitle(vo.getName());
		po.setImg(vo.getPic1());
		po.setSmallImg(vo.getPic2());
		po.setMallid(vo.getMall());
		po.setContent(vo.getContent());
		String abbrContent = HtmlUtils.text(vo.getContent());
		if (abbrContent.length() >= 50) {
			po.setAbbrContent(abbrContent.substring(0, 47) + "...");
		} else {
			po.setAbbrContent(abbrContent);
		}
		po.setJoinAddress(mall.getAddress());
		po.setStarttime(DateUtils.parseTimestamp(vo.getStartTime(),
				APIConstants.PC_TIME_FORMAT));
		po.setEndtime(DateUtils.parseTimestamp(vo.getEndTime(),
				APIConstants.PC_TIME_FORMAT));
		if (po.getStarttime() > po.getEndtime()) {
			throw new RuntimeException("结束时间比开始时间大");
		}
		po.setAuthor(user.getName());
		po.setVideoHtml(vo.getVideoHtml());
		
		long id = infoDAO.add(po);
		return id;
	}
	
	public String edit(AddInfoVO vo, BUser user) {
		Mall mall = mallDAO.findById(vo.getMall());
		if (mall == null) {
			return "mall not exists";
		}
		Info po = new Info();
		po.setId(vo.getId());
		po.setCategory(vo.getCategory());
		po.setUserid(user.getId());
		po.setTitle(vo.getName());
		po.setImg(vo.getPic1());
		po.setSmallImg(vo.getPic2());
		po.setMallid(vo.getMall());
		po.setContent(vo.getContent());
		String abbrContent = HtmlUtils.text(vo.getContent());
		if (abbrContent.length() >= 20) {
			po.setAbbrContent(abbrContent.substring(0, 17) + "...");
		} else {
			po.setAbbrContent(abbrContent);
		}
		po.setJoinAddress(mall.getAddress());
		po.setStarttime(DateUtils.parseTimestamp(vo.getStartTime(),
				APIConstants.PC_TIME_FORMAT));
		po.setEndtime(DateUtils.parseTimestamp(vo.getEndTime(),
				APIConstants.PC_TIME_FORMAT));
		if (po.getStarttime() > po.getEndtime()) {
			throw new RuntimeException("结束时间比开始时间大");
		}
		po.setAuthor(user.getName());
		po.setVideoHtml(vo.getVideoHtml());
		infoDAO.updateByPC(po);
		return null;
	}
	
	public int bindCoupon(long id, long couponid) {
		return infoDAO.bindCoupon(id, couponid);
	}
}
